<template>
  <div>
    <el-row style="margin-top: 50px">
      <el-col :span="12">
        <div>
          <el-form :inline="true" :model="params" class="demo-form-inline">
            <el-input
              v-model="params.value"
              placeholder="请输入药品名称"
              style="width: 300px"
            ></el-input>

            <el-select v-model="params.DrugsType" placeholder="药品分类">
              <el-option label="中草药" value="中草药"></el-option>
              <el-option label="中成药" value="中成药"></el-option>
            </el-select>

             <el-button type="primary" @click="toaddDrugs">导入药品信息</el-button> 
              <el-button type="primary" @click="search">检索</el-button> 
          </el-form>
        </div>
      </el-col>
    </el-row>

    <div>
      <el-table :data="pageResult.records" tripe style="width: 100%">
        <el-table-column
          prop="medicinesId"
          label="药品序号"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="medicinesName"
          label="药品名称"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="medicinesClass"
          label="药品分类"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="conversion"
          label="药品数量"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="unit"
          label="计量单位"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建日期"
          width="200"
        ></el-table-column>

        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
          <el-button
            type="text"
            @click="delDrugs(scope.row.medicinesId)"
            size="small"
            >删除</el-button
          >
          </template>
        </el-table-column>


        
   


      </el-table>

      <!--分页控制器-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageResult.current"
        :page-sizes="[5, 10, 15, 20]"  
        :page-size="pageResult.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageResult.total"
      >
      </el-pagination>
    </div>

    <el-dialog title="添加药品" :visible.sync="MedicinesAdd">
                <el-form :model="params.Drugsparams" label-width="100px">
                  <el-form-item label="药品编号">
                        <el-input v-model="params.Drugsparams.medicinesNumber" readonly></el-input>
                    </el-form-item>
                    <el-form-item label="药品名称">
                        <el-input v-model="params.Drugsparams.medicinesName"></el-input>
                    </el-form-item>
                    <el-form-item label="换算量">
                        <el-input-number v-model="params.Drugsparams.conversion"></el-input-number>
                    </el-form-item>
                    <el-form-item label="计量单位">
                        <el-input v-model="params.Drugsparams.unit"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="MedicinesAdd = false">取 消</el-button>
                    <el-button type="primary" @click="addDrugs">确 定</el-button>
                </div>
            </el-dialog>
  </div>
</template>

<script>
import Drugs from "../../model/rzw/HospitalDrugs.js";
const drugs = Drugs.getInstance();

export default {
  name: "Drugs.vue",

  data() {
    return {
      pageResult: {}, //page对象
      params: {
        size: 5,
        page: 1,
        medicinesName:"",
        DrugsType:"",
        Drugsparams:{
          medicinesNumber: ''
        }
      }, //高级查询参数对象
      MedicinesAdd: false
    };
  },

  methods:{
    list() {
      drugs.list(this.params).then((response) => {
        this.pageResult = response.data.data;
        
      });
    },

    search() {
      (this.params.page = 1), this.list();
    },
    
    handleSizeChange(size){
      this.params.size = size
      this.list()
    },

    handleCurrentChange(page){
      this.params.page = page
      this.list()
    },

    delDrugs(medicinesId){
        drugs.delDrugs(medicinesId).then(()=>{
          this.list()
      });
    },

     toaddDrugs(){
        this.getMaxCode();
        this.MedicinesAdd = true
    },

    addDrugs(){
                this.MedicinesAdd = false
                this.params.Drugsparams.prescriptionType = '中药处方'
                this.params.Drugsparams.medicinesClass = '中草药'
                drugs.add(this.params.Drugsparams).then(()=>{
                        this.list();
                        this.params.Drugsparams = {
                          medicinesNumber: '',
                        }
                })
         },
    getMaxCode(){
      drugs.getMaxCode().then(({data}) => {
        if(data.code === 0){
          this.params.Drugsparams.medicinesNumber = data.message
        }
      })
    }

  },
  created() {
    this.list();
  },
  
};
</script>